package com.example.agrigreennewssys.controller;


import com.example.agrigreennewssys.entity.Tag;
import com.example.agrigreennewssys.service.TagService;
import com.example.agrigreennewssys.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 光的代言人
 */
@RestController
@RequestMapping("/api/tags")
public class TagController {
    @Autowired
    private TagService tagService;

    // 创建标签
    @GetMapping("/add")
    public Result createTag(@RequestParam String name) {
        Tag tag = new Tag();
        tag.setName(name);
        Tag createdTag = tagService.createTag(tag);
        return Result.success(createdTag);
    }

    // 更新标签
    @PutMapping("/{id}")
    public Result updateTag(@PathVariable Long id, @RequestBody Tag tag) {
        tag.setId(id);
        Tag updatedTag = tagService.updateTag(tag);
        return Result.success(updatedTag);
    }

    // 删除标签
    @DeleteMapping("/{id}")
    public Result deleteTag(@PathVariable Long id) {
        boolean deleted = tagService.deleteTag(id);
        if (deleted) {
            return Result.success();
        }
        return Result.error(HttpStatus.NOT_FOUND.value(), "标签不存在");
    }

    // 获取标签详情
    @GetMapping("/{id}")
    public Result getTagById(@PathVariable Long id) {
        Tag tag = tagService.getTagById(id);
        if (tag != null) {
            return Result.success(tag);
        }
        return Result.error(HttpStatus.NOT_FOUND.value(), "标签不存在");
    }

    // 获取所有标签
    @GetMapping("/")
    public Result getAllTags() {
        List<Tag> tags = tagService.getAllTags();
        return Result.success(tags);
    }
}